package com.example.crm.controller;

import com.example.crm.entity.Chance;
import com.example.crm.service.ChanceService;
import com.example.crm.util.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/chance")
public class ChanceController {
    @Resource
    private ChanceService chanceService;

    @RequestMapping("/toadd")
    public String toadd(){
        return "chance/add";
    }

    @RequestMapping("/list")
    public String finAll(Model model,String custName,String chcDesc,Integer currPageNo){
        Integer page1 = currPageNo==null?1:currPageNo;
        Page page = new Page();
        page.setTotal(chanceService.count(custName,chcDesc)); // 数据总条数
        page.setCurrent(page1); // 设置偏移量

        // 存储数据
        List<Chance> all = chanceService.finNameAndTitle(custName, chcDesc, page.getOffset(), page.getPageSize());
        page.setList(all);

        model.addAttribute("chancePage",page);

        return "chance/list";
    }

    @RequestMapping("/toedit")
    public String finNameAndTitle(Integer chcId,Model model){
        Chance chance = chanceService.finId(chcId);
        model.addAttribute("chance",chance);
        return "chance/edit";
    }

    @RequestMapping("/edit")
    public String uplChance(Chance chance,Model model){
        int upl = chanceService.updChance(chance);
        if (upl<=0){
            return "chance/edit";
        }else {
            return "redirect:/chance/list";
        }
    }

    @RequestMapping("/add")
    public String addChance(Chance chance,Model model){
        int add = chanceService.allChance(chance);
        if (add<=0){
            return "chance/add";
        }else {
            return "redirect:/chance/list";
        }
    }

    @RequestMapping("/del/{chcId}")
    @ResponseBody
    public String delChance(@PathVariable(name = "chcId") Integer id){
        int del = chanceService.delChance(id);
        if (del>0){
            return "{\"delResult\",\"true\"}";
        }else {
            return "{\"delResult\",\"false\"}";
        }
    }




}
